/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.Locations;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.SQLServerDB;

/**
 *
 * @author Hai Muoi Thang Sau
 */
public class LocationDAO {

    String SQL_CREATE = "INSERT INTO Locations Values(?,?)";
    String SQL_SEARCHBYID = "SELECT * From Locations WHERE ID=?";
    String SQL_READALL = "SELECT * FROM Locations";
    String SQL_UPDATE = "UPDATE Locations Set Name=?, Address=? WHERE ID =?";
    String SQL_DELETE = "DELETE FROM Locations WHERE ID=?";
    private Connection conn;
    private String lastError;

    public LocationDAO() {

    }

    public boolean create(Locations a) {
        conn = SQLServerDB.getConn();
        try {

            PreparedStatement pr = conn.prepareStatement(SQL_CREATE);
            pr.setString(1, a.getAddress());
            pr.setInt(2, a.getTypeId());
            if (pr.executeUpdate() > 0) {
                setLastError("Create Successful!");
                return true;
            } else {
                setLastError("Error: Create Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public ArrayList<Locations> readAll() {
        conn = SQLServerDB.getConn();
        ArrayList<Locations> loca = new ArrayList<>();
        try {
            SQLServerDB db = new SQLServerDB();
            PreparedStatement pr = conn.prepareStatement(SQL_READALL);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Locations lo = new Locations();
                lo.setId(rs.getInt("ID"));
                lo.setAddress(rs.getString("Address"));
                lo.setTypeId(rs.getInt("TypeId"));
                loca.add(lo);
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return loca;
    }

    public Locations readById(int id) {
        conn = SQLServerDB.getConn();
        Locations l = new Locations();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_SEARCHBYID);
            pr.setInt(1, id);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                l.setId(rs.getInt("ID"));
                l.setAddress(rs.getString("Address"));
                l.setTypeId(rs.getInt("TypeId"));
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return l;
    }

    public boolean update(Locations a) {
        conn = SQLServerDB.getConn();
        try {
            PreparedStatement pr;
            pr = conn.prepareStatement(SQL_UPDATE);
            pr.setString(1, a.getAddress());
            pr.setInt(2, a.getTypeId());
            pr.setInt(3, a.getId());
            if (pr.executeUpdate() > 0) {
                setLastError("Update Successful!");
                return true;
            } else {
                setLastError("Error: Update Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public boolean delete(int id) {
        conn = SQLServerDB.getConn();
        try {
            
            PreparedStatement pr;
            pr = conn.prepareStatement(SQL_DELETE);
            pr.setInt(1, id);
            if (pr.executeUpdate() > 0) {
                setLastError("Delete Successful!");
                return true;
            } else {
                setLastError("Error: Delete Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public static void main(String[] args) {
        LocationDAO dao = new LocationDAO();
        ArrayList<Locations> locations = dao.readAll();
        Locations a = new Locations();
        a.setId(1);
        a.setAddress("Hue");
        a.setTypeId(1);
        System.out.println(dao.create(a));
        System.out.println(dao.getLastError());
    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }

}
